﻿using AutoMapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Singer.Middleware.AutoMapper
{
    public static class CoreMapperServiceExtensions
    {
        /// <summary>
        /// 注入指定的 mapper profile
        /// </summary>
        public static void AddCoreMapper(this IServiceCollection services, params Type[] profileTypes)
        {
            MapperConfiguration config = new MapperConfiguration(x =>
            {
                foreach (Type profileType in profileTypes)
                {
                    if (!typeof(Profile).IsAssignableFrom(profileType))
                        throw new Exception($"【CoreMapper】The Type '{profileType.Name}' is not an implementation of 'AutoMapper.Profile'.");
                    x.AddProfile(profileType);
                }
            });
            CoreMapper mapper = new CoreMapper(config);
            services.AddSingleton<ICoreMapper>(mapper);
        }
    }
}
